<?php

class Application_Model_JobExecute_Url extends Application_Model_JobExecute_Base {
    
    public function execute($job)
    {
        if(!$job->Operator){
            $isExecute = $job->getNeedRun();
            if(!$isExecute){
                return false;
            }
        }
        $jobLogItem = $this->start($job);
        try {
            $ch = curl_init($job->content); 
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $html = curl_exec($ch);
            curl_close($ch);
            $job->lastStatus  = Application_Model_Job::LastStatus_Succeed;
            $jobLogItem->status = Application_Model_Job::LastStatus_Succeed;
        } catch (Exception $exc) {
            $job->lastStatus  = Application_Model_Job::LastStatus_Fail;
            $jobLogItem->status = Application_Model_Job::LastStatus_Fail;
        }
        $this->end($jobLogItem);
        $job->count = $job->count + 1;
        $job->dateLast = date("Y-m-d H:i:s");
        $job->save();
    }
    protected function start($job) {
        $jobLogItem = new Application_Model_Joblog();
        $jobLogItem->JobID = $job->id;
        $jobLogItem->StartTime = date("Y-m-d H:i:s");
        $jobLogItem->CountTime = $job->count + 1;
        $jobLogItem->content = $job->Operator?$job->Operator."手工运行":"系统自主运行";
        return $jobLogItem;
    }
    
    protected function end($jobLogItem) {
        $jobLogItem->StopTime = date("Y-m-d H:i:s");
        $jobLogItem->save();
    }
    
    
}